package com.xs.ai.services;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.xs.ai.data.BookingStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import java.time.LocalDate;
import java.util.function.Function;

@Configuration
public class BookingTools {

    private static final Logger logger = LoggerFactory.getLogger(BookingTools.class);

    @Autowired
    FlightBookingService flightBookingService;

    @JsonInclude(Include.NON_NULL)
    public record BookingDetails(@JsonPropertyDescription("预订号") String bookingNumber,
                                 @JsonPropertyDescription("乘客姓名") String name, LocalDate date,
                                 BookingStatus bookingStatus,
                                 String from, String to, String bookingClass) {
    }

    public record CancelBookingRequest(@JsonPropertyDescription("需要取消的预订号") String bookingNumber,
                                       @JsonPropertyDescription("乘客姓名") String name) {
    }

    public record BookingDetailsRequest(@JsonPropertyDescription("需要查询的预订号") String bookingNumber,
                                        @JsonPropertyDescription("乘客姓名") String name) {
    }

    public record ChangeBookingRequest(@JsonPropertyDescription("需要修改的预订号") String bookingNumber,
                                       @JsonPropertyDescription("乘客姓名") String name,
                                       @JsonPropertyDescription("新日期(格式: yyyy-MM-dd)") String newDate,
                                       @JsonPropertyDescription("出发地") String from,
                                       @JsonPropertyDescription("目的地") String to) {
    }

    @Bean
    @Description("修改机票预订信息")
    public Function<ChangeBookingRequest, String> changeBooking() {
        return changeBookingRequest -> {
            logger.info("AI调用修改预订接口开始: 参数bookingNumber={}, 参数name={}, 参数newDate={}, 参数from={}, 参数to={}",
                    changeBookingRequest.bookingNumber(), changeBookingRequest.name(),
                    changeBookingRequest.newDate(), changeBookingRequest.from(), changeBookingRequest.to());

            try {
                flightBookingService.changeBooking(
                        changeBookingRequest.bookingNumber(),
                        changeBookingRequest.name(),
                        changeBookingRequest.newDate(),
                        changeBookingRequest.from(),
                        changeBookingRequest.to()
                );
                logger.info("修改预订成功: 参数bookingNumber={}, 参数name={}",
                        changeBookingRequest.bookingNumber(), changeBookingRequest.name());
                return "预订修改成功!";
            } catch (Exception e) {
                logger.error("修改预订失败: 参数bookingNumber={}, 参数name={}, 错误信息={}",
                        changeBookingRequest.bookingNumber(), changeBookingRequest.name(), e.getMessage(), e);
                return "修改预订失败: " + e.getMessage();
            }
        };
    }

    @Bean
    @Description("处理机票退订")
    public Function<CancelBookingRequest, String> cancelBooking() {
        return cancelBookingRequest -> {
            // 记录AI调用取消预订接口的详细信息
            logger.info("AI调用取消预订接口开始: 参数bookingNumber={}, 参数name={}", cancelBookingRequest.bookingNumber(), cancelBookingRequest.name());

            try {
                // apply 调用退订方法
                flightBookingService.cancelBooking(cancelBookingRequest.bookingNumber(), cancelBookingRequest.name());
                logger.info("退订成功: 参数bookingNumber={}, 参数name={}", cancelBookingRequest.bookingNumber(), cancelBookingRequest.name());
                return "退订成功!";
            } catch (Exception e) {
                logger.error("退订失败: 参数bookingNumber={}, 参数name={}, 错误信息={}", cancelBookingRequest.bookingNumber(), cancelBookingRequest.name(), e.getMessage(), e);
                return "退订失败: " + e.getMessage();
            }
        };
    }


    @Bean
    @Description("获取机票预定详细信息")
    public Function<BookingDetailsRequest, BookingDetails> getBookingDetails() {
        return request -> {
            logger.info("AI调用获取预订详情接口开始: 参数bookingNumber={}, 参数name={}", request.bookingNumber(), request.name());

            try {
                BookingDetails details = flightBookingService.getBookingDetails(request.bookingNumber(), request.name());
                logger.info("获取预订详情成功: 参数bookingNumber={}, 参数name={}, 返回结果={}", request.bookingNumber(), request.name(), details);
                return details;
            } catch (Exception e) {
                logger.error("获取预订详情失败: 参数bookingNumber={}, 参数name={}, 错误信息={}", request.bookingNumber(), request.name(), e.getMessage(), e);
                return new BookingDetails(request.bookingNumber(), request.name(), null, null, null, null, null);
            }
        };
    }

}
